let dbPromise = null;
function openDB() {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open('mhg-db', 1);
    request.onerror = (event) => {
      reject(event.target.error);
      console.error('IndexedDB error:', event.target.error);
    };
    request.onsuccess = (event) => {
      resolve(event.target.result);
    };
    request.onupgradeneeded = (event) => {
      const db = event.target.result;
      // 创建对象存储区（object store）
      const bookmarksStore = db.createObjectStore('bookmarks', { keyPath: 'id', autoIncrement: true });
      bookmarksStore.createIndex('title', 'title', { unique: false }); // 创建索引，方便根据标题搜索
      bookmarksStore.createIndex('category', 'category', { unique: false }); // 创建索引，方便根据分类搜索
    };
  });
}

openDB().then((db) => {
  dbPromise = db;
  console.log('IndexedDB opened successfully');
}).catch((error) => {
  console.error('IndexedDB error:', error);
});